<?php
namespace app\api\controller;

use app\api\common\controller\Api;
use hg\apidoc\annotation as Apidoc;
use app\common\library\EasyWechat;
use think\cache\driver\Redis;
use think\facade\Config;
use think\facade\Cache;
use app\api\common\controller\LuckDraw;
use think\facade\Db;
use think\facade\Request;

/**
 * @Apidoc\Title("Demo.php")
 * @Apidoc\Group("api")
 */
class Todays extends Api
{

    /**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = ['*'];
    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = [];
     // 今日业绩
    public function todayAchievement (){
        $start_time =input('start_time')?strtotime(input('start_time')): strtotime(date('Y-m-d 00:00:00'));
        $end_time =input('end_time')?strtotime(date('Y-m-d 23:59:59',strtotime(input('end_time')))):  strtotime(date('Y-m-d 23:59:59'));
     
         $Flow=$this->getTodayWater($start_time,$end_time);//流水统计
    
        $data=[
            'Flow' =>$Flow,
        ];
        $this->result('success',$data,200,'json');
    }
    /**
     * 今日业绩流水统计 function
     * @Author YJ 991850594@qq.com
     * @DateTime 2021-01-26
     * @param [type] $start_time
     * @param [type] $end_time  1-扫码支付2-现金支付3-小程序支付（用户自行支付）4-年卡支付
     * @return void
     */
    public function getTodayWater($start_time,$end_time){
        $scan_price=$this->getAllWaterCount($start_time,$end_time,1);
        $cash_price=$this->getAllWaterCount($start_time,$end_time,2);
        $xcx_price=$this->getAllWaterCount($start_time,$end_time,3);
        $total_price=0;
        $receipts=0;
        $total_price=$scan_price['activity_price']+$cash_price['activity_price']+$xcx_price['activity_price'];
        $receipts=$scan_price['price']+$cash_price['price']+$xcx_price['price'];

        //流水统计
        $Flow=[
            //总流水
            'total_price'  =>number_format($total_price,2),
            //应收总额
            'receivable'   =>number_format($receipts,2),
            //实收金额
            'receipts'     =>number_format($total_price,2),
            'scan_price'   => $scan_price['activity_price'],   // 扫码价格
            'cash_price'   => $cash_price['activity_price'],  // 现金价格
            'xcx_price'    => $xcx_price['activity_price'],   // 小程序价格
        ];
        return $Flow;
    }
    public function getAllWaterCount($start_time,$end_time,$pay_method){
        $where=[
            ['orders_status','in',[1,2]],
            ['create_time','between',[$start_time,$end_time]],
            ['pay_method','=',$pay_method]
        ];
        $price=0;
        $activity_price=0;
        $orderIds=Db::name('order')->where($where)->column('id');
        $order_server=Db::name('order_server')->whereIn('order_id',$orderIds)->select()->toArray();
        foreach($order_server as $k=>$v){
            $price+=$v['price']*$v['number'];
            $activity_price+=$v['act_price']*$v['number'];
        }
        return [
            'price'=>$price,
            'activity_price'=>$activity_price,
        ];
    }
    public function getOrders(){
        $start_time =input('start_time')?strtotime(input('start_time')): strtotime(date('Y-m-d 00:00:00'));
        $end_time =input('end_time')?strtotime(date('Y-m-d 23:59:59',strtotime(input('end_time')))):  strtotime(date('Y-m-d 23:59:59'));
        $page=input('page')?input('page'):1;
         $size=input('size')?input('size'):20;
        $pay_method=input('pay_method');//1-扫码支付2-现金支付3-小程序支付
        $where=[
            ['orders_status','in',[1,2]],
            ['create_time','between',[$start_time,$end_time]],
            ['pay_method','=',$pay_method]
        ];
        $order_id=Db::name('order')->where($where)->column('id');
        $order_server=Db::name('order_server')->whereIn('order_id',$order_id)->page($page,$size)->select()->toArray();
        $data=[];
        foreach ($order_server as $k => $v) {
              if($v['type']==1){
                        $ticket=Db::name('ticket')->where('id',$v['server_id'])->field('title,thumb')->find();
                        $data[$k]['title']=$ticket['title'];
                }elseif($v['type']==2){
                    $lease=Db::name('lease')->where('id',$v['server_id'])->find();
                     $data[$k]['title']=$lease['title'];
                }elseif($v['type']==3){
                    $goods=Db::name('goods')->where('products_id',$v['server_id'])->find();
                    $data[$k]['title']=$goods['products_title'];
                }elseif($v['type']==4){
                     $package=Db::name('package')->where('id',$v['server_id'])->find();
                     $data[$k]['title']=$package['title'];
                }
                $data[$k]['id']=$v['order_id']; 
                $data[$k]['price']=Db::name('order')->where('id',$v['order_id'])->value('price'); 
        }
        // $data=Db::name('order')->where($where)->field('id,price,out_trade_no')->page($page,$size)->select()->toArray();
        // foreach ($data as $k => $v) {
        //      $order_server=Db::name('order_server')->where('order_id',$v['id'])->find();
        //      if($order_server['type']==1){
        //                 $ticket=Db::name('ticket')->where('id',$order_server['server_id'])->field('title,thumb')->find();
        //                 $data[$k]['title']=$ticket['title'];
        //         }elseif($order_server['type']==2){
        //             $lease=Db::name('lease')->where('id',$order_server['server_id'])->find();
        //              $data[$k]['title']=$lease['title'];
        //         }    
        // }
        $this->result('success',$data,200,'json');
    }
}